package com.core.config;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

/**
 * @Author: 孙常胜
 * @CreateTime: 2021/7/21 14:55
 * @Description:
 */
@Log4j2
@Service
public class ClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object body) throws Exception {
        log.info("我是客户端，收到服务端数据：{}", body);
        String msg="服务端你好";
        ByteBuf buf = Unpooled.buffer();
        buf.writeBytes(msg.getBytes());
        ctx.channel().writeAndFlush(buf);
        log.info("我是客户端，响应服务端：{}", msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
